package com.miguo.activity.callback.listener;

import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import com.miguo.activity.callback.IGoodsScroll;

public class GoodsScrollListener implements OnScrollListener {


    public interface OnScrollDirectionChangedListener{
        void onScrollDirectionChanged(Direction direction);
    }

    public enum Direction {
        UP(0),
        DOWN(1),
        TOP(2);

        private int value;

        Direction(int value){
            this.value = value;
        }

        public int getValue(){
            return this.value;
        }

    }

    private IGoodsScroll mCallBack;
    private int mScrollPosition;
    private OnScrollDirectionChangedListener listener;

    public GoodsScrollListener(IGoodsScroll callback) {
        mCallBack = callback;
    }

    public void setOnScrollDirectionChangedListener(OnScrollDirectionChangedListener listener){
        this.listener = listener;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case OnScrollListener.SCROLL_STATE_IDLE:
                if(listener != null) {
                    View topChild = view.getChildAt(0);
                    int newScrollPosition = 0;
                    if (topChild == null) {
                        newScrollPosition = 0;
                    } else {
                        newScrollPosition = -topChild.getTop() + view.getFirstVisiblePosition() * topChild.getHeight();
                    }
                    if (newScrollPosition < mScrollPosition) {
                        listener.onScrollDirectionChanged(Direction.DOWN);
//                        Log.d("dc", "下滑");
                    } else {
                        listener.onScrollDirectionChanged(Direction.UP);
//                        Log.d("dc", "上滑");
                    }
                    mScrollPosition = newScrollPosition;
                }
                if (view.getFirstVisiblePosition() == View.VISIBLE) {
                    // 判断滚动到顶部
                    mCallBack.onScrollTopPositiont();
                } else if (view.getLastVisiblePosition() != (view
                        .getCount() - 1)) {
                    // 第一次拖至底部
                    mCallBack.onScrollFirstLastPosition();
                } else if (view.getLastVisiblePosition() == (view
                        .getCount() - 1)) {
                    // 判断滚动到底部
                    mCallBack.onScrollLastPosition();
                }
                mCallBack.onScrollIdle();
                break;
            case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                mCallBack.onScrollTouchScroll();
                break;
            case OnScrollListener.SCROLL_STATE_FLING:
                mCallBack.onScrollFling();
                break;
        }
    }

}
